package com.yao.helper;

import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import com.base.widget.LoadingDialog;

public class DialogFragmentHelper {
    private static final String DIALOG_TAG_CONFIRM = "dialog_confirm";//确定取消提示框
    private static final String DIALOG_TAG_TIPS = "dialog_tips";
    private static final String DIALOG_TAG_LOADING = "dialog_loading";


    //显示加载对话框
    public static void showLoadingDialog(FragmentManager fragmentManager) {
        showLoadingDialog(fragmentManager, null);
    }

    //显示加载对话框
    public static void showLoadingDialog(FragmentManager fragmentManager, String message) {
        showLoadingDialog(fragmentManager, message, true, DIALOG_TAG_LOADING);
    }

    //显示加载对话框
    public static void showLoadingDialog(FragmentManager fragmentManager, String message, boolean cancelable, String tag) {
        closeLoadingDialog(fragmentManager);
        LoadingDialog dialogFragment = new LoadingDialog();
        showDialogFragment(fragmentManager, dialogFragment, tag);
    }

    //关闭加载对话框
    public static void closeLoadingDialog(FragmentManager fragmentManager) {
        removeFragment(fragmentManager, DIALOG_TAG_LOADING);
    }

    //显示自定义对话框
    public static void showDialogFragment(FragmentManager fragmentManager, DialogFragment dialogFragment, String tag) {
        if (!fragmentManager.isDestroyed()) {
            try {
                removeFragment(fragmentManager, tag);
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                transaction.add(dialogFragment, tag);
                transaction.commitAllowingStateLoss();
//        dialogFragment.show(fragmentManager, tag);    `//直接用show方法可能会抛异常Can not perform this action after onSaveInstanceState
            } catch (Throwable th) {
                th.printStackTrace();
            }
        }
    }

    public static void showFragment(FragmentManager fragmentManager, Fragment fragment, String tag) {
        if (!fragmentManager.isDestroyed()) {
            try {
                removeFragment(fragmentManager, tag);
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                transaction.add(fragment, tag);
                transaction.commitAllowingStateLoss();
//        dialogFragment.show(fragmentManager, tag);    `//直接用show方法可能会抛异常Can not perform this action after onSaveInstanceState
            } catch (Throwable th) {
                th.printStackTrace();
            }
        }
    }

    public static Fragment getFragment(FragmentManager fragmentManager, String tag) {
        if (!fragmentManager.isDestroyed()) {
            try {
                Fragment fragment = fragmentManager.findFragmentByTag(tag);
                return fragment;
            } catch (Throwable th) {
                th.printStackTrace();
            }
        }
        return null;
    }

    //移除对话框
    public static void removeFragment(FragmentManager fragmentManager, String tag) {
        if (!fragmentManager.isDestroyed()) {
            try {
                FragmentTransaction ft = fragmentManager.beginTransaction();
                Fragment fragment = fragmentManager.findFragmentByTag(tag);
                if (fragment != null && fragment.isAdded()) {
                    ft.remove(fragment);
                    ft.commitNowAllowingStateLoss();
                }
            } catch (Throwable th) {
                th.printStackTrace();
            }
        }
    }
}
